﻿#region LicenseHeader

// Copyright 2012 The Trustees of Indiana University.  All rights reserved.
// 
// The Indiana University licenses this file to you under the Apache License, 
// Version 2.0 (the "License"); you may not use this file except in compliance 
// with the License.  You may obtain a copy of the License at 
// 
// http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software 
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
// See the License for the specific language governing permissions and 
// limitations under the License.
// 
// @author Thilina Gunarathne (tgunarat@indiana.edu)

#endregion

#region

using System;
using System.IO.MemoryMappedFiles;

#endregion

namespace AzureMRCore.DataTypes
{
    /**
     * Used to store a whole matrix as a value.
     */

    public class MappedShortMatrixValue : IValue
    {
        private MemoryMappedViewAccessor _accessor;
        private long _position;
        public Int32 BlockHeight { get; set; }
        public Int32 Width { get; set; }
        // RowBlock block number
        public Int32 RowBlock { get; set; }
        // actualy row offset
        public Int32 RowOffset { get; set; }

        #region IValue Members

        public string GetTextValue()
        {
            throw new NotImplementedException();
        }

        public bool ParseValue(object value)
        {
            /* if (value is TYPE[][])
            {
                Matrix = (TYPE[][]) value;
                return true;
            }*/
            return false;
        }

        public byte[] GetBytes()
        {
//            if (typeof (TYPE) != typeof (double))
//            {
            throw new NotImplementedException();
//            }
//
//            byte[] bytes = new byte[Width*BlockHeight*8 + 16];
//            Buffer.BlockCopy(BitConverter.GetBytes(BlockHeight), 0, bytes, 0, 4);
//            Buffer.BlockCopy(BitConverter.GetBytes(Width), 0, bytes, 4, 4);
//            Buffer.BlockCopy(BitConverter.GetBytes(RowBlock), 0, bytes, 8, 4);
//            Buffer.BlockCopy(BitConverter.GetBytes(RowOffset), 0, bytes, 12, 4);
//            for (int j = 0; j < BlockHeight; j++)
//            {
//                Buffer.BlockCopy(Matrix[j], 0, bytes, (j*Width*8) + 16, Width*8);
//            }
//            return bytes;
        }

        public bool FromBytes(byte[] value)
        {
//            if (typeof (TYPE) != typeof (double))
//            {
            throw new NotImplementedException();
//            }
//
//            BlockHeight = BitConverter.ToInt32(value, 0);
//            Width = BitConverter.ToInt32(value, 4);
//            RowBlock = BitConverter.ToInt32(value, 8);
//            RowOffset = BitConverter.ToInt32(value, 12);
//
//            Matrix = new TYPE[BlockHeight][];
//            for (int j = 0; j < BlockHeight; j++)
//            {
//                Matrix[j] = new TYPE[Width];
//                Buffer.BlockCopy(value, (j*Width*8) + 16, Matrix[j], 0, Width*8);
//            }
//            return true;
        }

        public long Size()
        {
            return BlockHeight*Width*8;
        }

        #endregion

        public void GetRow(int rowIndex, ref short[] row)
        {
            _accessor.ReadArray(_position, row, 0, Width);
            _position += sizeof (short)*Width;
        }

        public static MappedShortMatrixValue GetInstance(MemoryMappedViewAccessor accessor, long position, Int32 height,
                                                         Int32 width, Int32 row,
                                                         Int32 rowOffset)
        {
            var instance = new MappedShortMatrixValue
                {
                    _accessor = accessor,
                    _position = position,
                    BlockHeight = height,
                    Width = width,
                    RowBlock = row,
                    RowOffset = rowOffset
                };
            return instance;
        }
    }
}